function uniLogin() {
  return new Promise((success, fail) => {
    uni.login({
      success,
      fail,
    });
  });
}

export default {
  updateIndex({ dispatch, state }, data) {
    const { sid, list } = data;
    const last1 = list[list.length - 1];
    let lastMsg = {
      lastText: "",
      updateAt: last1.updateAt || last1.postAt,
      count: {},
      total: 0,
    };
    if (last1 && !last1.isMe) {
      lastMsg.lastText = last1.text;
    }
    for (const row of state.typeList) {
      const num = list.filter((it) => {
        return it.type == row.type && it.isMe;
      }).length;
      lastMsg.total += num;
      lastMsg.count[row.type] = num;
    }
    const indexList = state.indexList.map((it) => {
      if (it.sid == sid)
        return {
          ...it,
          ...lastMsg,
        };
      return it;
    });
    return dispatch("setStore", {
      indexList,
    });
  },
  async mpLogin({ dispatch, state }) {
    const { userInfo } = state;
    if (userInfo.openid) return;
    // #ifdef H5
    if (Date)
      return dispatch("setStore", {
        userInfo: {
          ...userInfo,
          openid: "test-id",
        },
      });
    // #endif
    // 一个微信账号绑定一个邮箱，不同邮箱可各自登录
    const { code } = await uniLogin();
    const { data } = await uni.$http.post(
      "https://tablo.fun/api-v2/third/wx/mp-login",
      {
        pid: 16,
        code,
      }
    );
    return dispatch("setStore", {
      userInfo: {
        ...userInfo,
        ...data, //openid unionid session_key
      },
    });
  },
};
